from django.shortcuts import render
from webapp import models

from django.http import HttpResponse
from django.template import  loader

from .models import UserInfo
# Create your views here.

def index(request):
    if request.method == "POST":
        username = request.POST.get("username",None)
        password = request.POST.get("password",None)
        # 添加数据到数据库
        models.UserInfo.objects.create(user=username,pwd=password)
    # 从数据库中读取所有数据
    user_list = models.UserInfo.objects.all()
    return render(request,'index.html',{"data":user_list})
def ok(request):
    user_list = UserInfo.objects.order_by('user')[:5]

    template = loader.get_template('webapp/ok.html')
    context = {
        'user_list':user_list,
    }
    return HttpResponse(template.render(context,request))

def hello(request):
    username = request.GET['username']
    print(username)
    userVo = models.UserInfo.objects.get(user=username)
    print(userVo.pwd)
    result = (
        username,
        userVo.pwd,
    )
    return HttpResponse(result)